home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-01-09 | 5.7 KB | 196 lines | [TEXT/MMCC] |
- // Current Printer info sample code
- //
- // Dave Polaschek and David Hayward
- // Developer Technical Support
- // AppleLink: DEVSUPPORT
- //
- // Copyright 1995 - 1996, Apple Computer,Inc
- //
- // Check the default printer under GX or old printing architecture.
- // Print out the name, entityType, and zone.
- //
- // This sample is provided as-is. It's guaranteed to work on my
- // mac this instant, and nothing more. It shows how to determine
- // the currently selected printer now. Since this isn't an officially
- // supported endeavor, things may change in the future, and this
- // snippet will be for naught.
- //
- //
- // You may incorporate this sample code into your applications without
- // restriction, though the sample code has been provided "AS IS" and the
- // responsibility for its operation is 100% yours. However, what you are
- // not permitted to do is to redistribute the source as "DSC Sample Code"
- // after having made changes. If you're going to re-distribute the source,
- // we require that you make it clear in the source that the code was
- // descended from Apple Sample Code, but that you've made changes.
- //
-
- // Revision history
- //
- // davep 8/16/95 initial cut
- // davep 1/9/96 cleanup and add comments
-
-
- #include <stdio.h>
-
- #define gestaltGXVersion 'qdgx' // in PrintingManager.h (old) or GXPrinting.h (new)
- #define gestaltGXPrintingMgrVersion 'pmgr' // in PrintingManager.h (old) or GXPrinting.h (new)
- #define gestaltAliasMgrAttr 'alis' // in Aliases.h
-
- #define printingResourceID 0xE000 // 0xE000 = -8192
-
-
- OSErr GetCurrentPrinter(Str255 printer);
- OSErr test(void);
- Boolean gxInstalled(void);
- Boolean validPrinter(void);
-
-
- /*------------------------------------------------------------------------------*\
- This function returns true if QuickDraw GX printing is available
- \*------------------------------------------------------------------------------*/
- Boolean gxInstalled(void)
- {
- long version;
-
- if (Gestalt(gestaltGXVersion, &version) == noErr)
- if (Gestalt(gestaltGXPrintingMgrVersion, &version) == noErr)
- return(true);
- return(false);
- }
-
-
- /*------------------------------------------------------------------------------*\
- This function returns the network entity of the currently
- selected printer, and as an added bonus, you get an error code, too!
- \*------------------------------------------------------------------------------*/
- OSErr GetCurrentPrinter(Str255 printer)
- {
- StringHandle theDriver;
- Handle thePrinter;
- OSErr theErr;
- short resFileRefnum;
-
- theDriver = GetString(printingResourceID);
- if (!theDriver) goto BADDriver;
-
- if (gxInstalled()) {
- short vRefNum;
- long dirID;
- FSSpec theFile;
- long resSize;
-
- theErr=FindFolder(kOnSystemDisk,kDesktopFolderType,kDontCreateFolder,&vRefNum,&dirID);
- if (theErr != noErr) goto BADDriver;
-
- theErr=FSMakeFSSpec(vRefNum,dirID,*theDriver,&theFile);
- if (theErr != noErr) goto BADDriver;
-
- resFileRefnum = FSpOpenResFile(&theFile,fsRdPerm);
- if (resFileRefnum == -1) goto BADResFile;
-
- thePrinter = Get1Resource('comm', 0);
- if (!thePrinter) goto BADPrinter;
-
- if (*((long *)*thePrinter) != 'PPTL') goto BADPrinter;
-
- resSize = GetHandleSize(thePrinter)-6;
- BlockMoveData((*thePrinter)+6,printer,resSize);
- } else {
- Boolean aliasCallsPresent;
- Boolean aliasResourcePresent;
- long resSize,response;
- AliasHandle theAlias;
-
- aliasCallsPresent = ((Gestalt(gestaltAliasMgrAttr,&response) == noErr) &&
- (response & 1));
-
- theAlias = (AliasHandle) GetResource('alis',printingResourceID);
- aliasResourcePresent = !!(theAlias);
-
- if (aliasCallsPresent && aliasResourcePresent) {
- FSSpec fileSpec;
- Boolean wasChanged;
-
- theErr = ResolveAlias(NULL, theAlias,&fileSpec,&wasChanged);
- if (theErr != noErr) goto BADDriver;
-
- resFileRefnum = FSpOpenResFile(&fileSpec,fsRdPerm);
- if (resFileRefnum == -1) goto BADResFile;
- } else {
- resFileRefnum = OpenResFile(*theDriver);
- if (resFileRefnum == -1) goto BADResFile;
- }
- thePrinter = Get1Resource('PAPA', printingResourceID);
- if (!thePrinter) goto BADPrinter;
- resSize = GetHandleSize(thePrinter);
- BlockMoveData(*thePrinter,printer,resSize);
- }
- ReleaseResource(thePrinter);
- CloseResFile(resFileRefnum);
-
- return(noErr);
-
- // Error returns
- BADPrinter:
- CloseResFile(resFileRefnum);
- BADResFile:
- theErr = ResError();
- BADDriver:
- return(theErr);
- }
-
-
- /*------------------------------------------------------------------------------*\
- This function is called by the test harness. Doesn't do much except
- prove that things work without crashing.
- \*------------------------------------------------------------------------------*/
- OSErr test(void)
- {
- OSErr testValue;
- Str255 printerEntity;
-
- testValue = GetCurrentPrinter(printerEntity);
- if (testValue)
- return testValue;
- else {
- Str31 printer,type,zone;
- unsigned char *currentString;
-
- currentString = printerEntity;
- BlockMove(currentString,printer,Length(currentString)+1);
- p2cstr(printer);
-
- currentString += Length(currentString) + 1;
- BlockMove(currentString,type,Length(currentString) + 1);
- p2cstr(type);
-
- currentString += Length(currentString) + 1;
- BlockMove(currentString,zone,Length(currentString) + 1);
- p2cstr(zone);
-
- printf("Current Printer name = %s\n"
- "EntityType = %s\n"
- "Zone = %s\n",printer,type,zone);
- return noErr;
- }
- }
-
-
- /*------------------------------------------------------------------------------*\
- This function tells if there's a valid printer selected (i.e. not
- aimed at a driver that's since been deleted or no printer selected
- since last System sw install) in the chooser
- \*------------------------------------------------------------------------------*/
- Boolean validPrinter(void)
- {
- Str255 printerEntity;
-
- if (GetCurrentPrinter(printerEntity) != noErr)
- return false;
- else if (Length(printerEntity) == 0)
- return false;
- else
- return true;
- }
-